package com.pap.oauth.authentication.mobile.config.security;

import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.provisioning.UserDetailsManager;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 *  自定义一个 UserDetailsManager， 用来通过 mobile 获取 UserDetails 对象，
 */
@Deprecated
// @Service
public class MobileUserDetailsService implements UserDetailsManager {

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        // 程序并不显示调用此方法，而是调用 自定义的如下方法。所以此处强制抛出异常。
        throw new UsernameNotFoundException("Username was not found. ");
    }

    /**
     * 自定义方法，根据 手机号、验证码 进行用户信息判断。
     * @param mobile
     * @param smsCode
     * @return
     * @throws UsernameNotFoundException
     */
    public UserDetails loadUserByMobileAndSmsCode(String mobile, String smsCode) throws UsernameNotFoundException {
        // TODO 后续这里可以通过 DB 进行用户数据查询。
        String password;
        List<GrantedAuthority> auth = AuthorityUtils.commaSeparatedStringToAuthorityList("ROLE_USER");
        if (mobile.equals("13800138000") && smsCode.equals("123456")) {
            auth = AuthorityUtils.commaSeparatedStringToAuthorityList("ROLE_PAP_MOBILE");
            password = "pap";
        }
        else{
            throw new UsernameNotFoundException("Username was not found. ");
        }
        return new org.springframework.security.core.userdetails.User(mobile, password, auth);
    }

    @Override
    public void createUser(UserDetails user) {// TODO Auto-generated method stub
    }

    @Override
    public void updateUser(UserDetails user) {// TODO Auto-generated method stub
    }

    @Override
    public void deleteUser(String username) {// TODO Auto-generated method stub
    }

    @Override
    public void changePassword(String oldPassword, String newPassword) {
        // TODO Auto-generated method stub
    }

    @Override
    public boolean userExists(String username) {
        // TODO Auto-generated method stub
        return false;
    }
}